package com.gabry;

import org.apache.phoenix.jdbc.PhoenixConnection;
import org.apache.phoenix.jdbc.PhoenixResultSet;
import org.apache.phoenix.jdbc.PhoenixStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author wusj
 */
public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);

    private static void init(PhoenixConnection connection) throws SQLException {
        try{
            connection.createStatement().execute("drop table if exists test");
        }catch (Exception e){
            e.printStackTrace();
        }
        connection.createStatement().execute("create table test( " +
                "id VARCHAR primary key," +
                "name varchar," +
                "email varchar)");
        connection.createStatement().execute("create index idx_test_email  on test(email)");
        connection.createStatement().execute("upsert into test(id,name,email) values('1','firstUser','test@test.com')");
    }

    private static void explain(PhoenixStatement statement,String sql){
        try {
            PhoenixResultSet explainRes = (PhoenixResultSet)statement.executeQuery("explain "+sql);
            while (explainRes.next()){
                logger.info("explain: {}",explainRes.getString(1));
            }
            explainRes.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private static void execSql(PhoenixConnection connection,String sql) throws SQLException{
        execSql(connection,sql,false);
    }
    private static void execSql(PhoenixConnection connection,String sql,boolean printResult ) throws SQLException {
        System.out.println(sql);
        PhoenixStatement statement = (PhoenixStatement)connection.createStatement();
        PhoenixResultSet resultSet = (PhoenixResultSet)statement.executeQuery(sql);
        if(printResult){
            while (resultSet.next()){
                logger.info("id: {},name: {},email: {}",
                        resultSet.getString("id"),
                        resultSet.getString("name"),
                        resultSet.getString("email"));
            }
        }

        resultSet.close();
        statement.close();
    }
    public static void main( String[] args ){
        try {
            PhoenixConnection connection = (PhoenixConnection)DriverManager.getConnection(
                    "jdbc:phoenix:"+args[0],
                    "test",
                    "test");
            init(connection);
            execSql(connection,"select * from test where email='test@test.com'");
            execSql(connection,"select /*+ INDEX(test idx_test_email) */ * from test where email='test@test.com'");
            execSql(connection,"select /*+ INDEX(test idx_test_email) */ * from test where email='test@test.com' and name != '123' ");
            execSql(connection,"select a.* from test a join test b on a.id = b.id where b.email='test@test.com'");
            execSql(connection,"select a.* from test a join test b on a.id = b.id where b.email='test@test.com' and a.name != '123'");
            execSql(connection,"select * from (select a.id,a.name,a.email from test a join test b on a.id = b.id where b.email='test@test.com') c where c.name != '123'");
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
